package top.chmin.project.cloud.common;

import cn.hutool.core.util.StrUtil;

/**
 * chmin
 * 2024/1/11 9:58
 * cloud-code
 */
public class ConditionColumn<T extends Condition> {
    public final String fieldName;
    public final String columnName;
    public final T t;

    public ConditionColumn(T t, String fieldName) {
        this.t = t;
        this.fieldName = fieldName;
        this.columnName = StrUtil.toUnderlineCase(fieldName);
    }

    public T group() {
        t.getGroupList().add(columnName);
        return t;
    }

    public T asc() {
        t.getOrderList().add(columnName);
        return t;
    }

    public T desc() {
        t.getOrderList().add(columnName + " DESC");
        return t;
    }

    public T count() {
        t.getSelectList().add("COUNT(%s)".formatted(columnName));
        return t;
    }

    public T min() {
        t.getSelectList().add("MIN(%s)".formatted(columnName));
        return t;
    }

    public T max() {
        t.getSelectList().add("MAX(%s)".formatted(columnName));
        return t;
    }

    public T sum() {
        t.getSelectList().add("SUM(%s)".formatted(columnName));
        return t;
    }

    public T avg() {
        t.getSelectList().add("AVG(%s)".formatted(columnName));
        return t;
    }
}
